package com.cms.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.model.TrAdminUser;
import com.cms.model.TrCustomerField;
import com.cms.service.CustomerFieldService;
import com.cms.service.ReportService;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

@Controller
public class CustomerFieldControrller extends BaseController{

	
	 @Autowired
	 private ReportService reportService;
	 
	 @Autowired
	 private CustomerFieldService customFieldService;
	
	 /***
	  * 
	  * @param req
	  * @param resp
	  * @param map
	  * @return
	  * @throws Exception
	  */
	 @RequestMapping("/customerFieldMain.do")
	 public String customerFieldMain(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {
		 Map<String,Object>param = this.buildQueryMap(req);
		 map.put("customerFieldList", this.reportService.getReportResult("queryCustomerFieldList", param));
		 map.putAll(param);
		 return "customerField/customerFieldList.html";
	 }
	 
	 
	 @RequestMapping("/saveCustomerFieldMain.do")
	 public void saveCustomerField(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {
		 String data= req.getParameter("data");
		 if(StringUtils.isBlank(data))
		 {
			 this.renderText(resp, CmsConstant.FAIL_STATUS);
			 return ;
		 }
		 String filedCatalog=req.getParameter("filedCatalog");
		 String fieldHandler= req.getParameter("fieldHandler");
		 List<TrCustomerField>customerFields = new ArrayList<TrCustomerField>();
         JsonArray array=(JsonArray) new JsonParser().parse(data);
         JsonObject obj = null;
         TrCustomerField customerField =null;
         TrAdminUser tu = this.getLoginUser(req);
         String value= null;
         for(int i=0;array!=null&&i<array.size();i++)
         {
        	 customerField=  new TrCustomerField();
             obj = array.get(i).getAsJsonObject();
        	 value =obj.get("value").getAsString();
        	 if(StringUtils.isBlank(value))
        	 {
        		 continue;
        	 }
             customerField.setCreateUser(tu.getId());
        	 customerField.setModifyUser(tu.getId());
        	 customerField.setFieldValue(value);
        	 customerField.setFieldCatalog(filedCatalog);
        	 customerField.setFieldName(obj.get("name").getAsString());
        	 customerField.setFieldHandler(fieldHandler);
        	 customerFields.add(customerField);
         }
         Map<String,Object>result =new HashMap<String, Object>();
         try
         {
           this.customFieldService.addCustomerField(fieldHandler,customerFields);
           result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS); 
         }catch (Exception e) {
        	  result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS); 
        	  result.put(CmsConstant.MSG, e.getMessage()); 
		 }
         this.renderText(resp, new Gson().toJson(result));
	 }
	 
	 
	 
	 
	
}
